package com.suxin.netty.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @author Tang
 * @classname MySimpleEncoder
 * @description [ TODO ]
 * @date 2021/1/11 21:16
 */
public class MySimpleDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MySimpleDecoder...解码成对象...");
        ProtocolMessage protocolMessage = new ProtocolMessage();
        // 读长度
        int len = in.readInt();
        // 读内容
        byte[] bytes = new byte[len];
        in.readBytes(bytes);
        protocolMessage.setLen(len);
        protocolMessage.setMsg(bytes);
        out.add(protocolMessage);
    }
}